🎖️GitЯра🎖️
Node / meshtastic / Meshtastic-Android / files / core / data / src / commonMain / kotlin / org / meshtastic / core / data / datasource / SwitchingChannelSetDataSource.kt
Displaying Raw • Download
core/data/src/commonMain/kotlin/org/meshtastic/core/data/datasource/SwitchingChannelSetDataSource.kt 01cd54907d62cd90913d3d071b6bc240cae365ef (01cd5490) Text, 4.20 KB
T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.datasource
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.distinctUntilChanged
Tff7b72import T7ee787kotlinx.coroutines.flow.flatMapLatest
Tff7b72import T7ee787kotlinx.coroutines.flow.map
Tff7b72import T7ee787kotlinx.coroutines.sync.Mutex
Tff7b72import T7ee787kotlinx.coroutines.sync.withLock
Tff7b72import T7ee787kotlinx.coroutines.withContext
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.database.DatabaseProvider
Tff7b72import T7ee787org.meshtastic.core.database.entity.ChannelSetEntity
Tff7b72import T7ee787org.meshtastic.core.di.CoroutineDispatchers
Tff7b72import T7ee787org.meshtastic.proto.Channel
Tff7b72import T7ee787org.meshtastic.proto.ChannelSet
Tff7b72import T7ee787org.meshtastic.proto.ChannelSettings
Tff7b72import T7ee787org.meshtastic.proto.Config
T8b949e/**
* Per-device channel-set storage backed by the current device's Room database.
*
* Replaces the former single global `channel_set.pb` DataStore. The channel set now switches with
* [DatabaseProvider.currentDb], exactly like messages and nodes, so switching between devices can no longer render one
* device's conversations against another device's channels (#4623).
*
* The whole [ChannelSet] proto is stored in one row (see [ChannelSetEntity]); mutations are read-modify-write, so they
* are serialized through [writeMutex] to preserve the atomicity the old DataStore's `updateData {}` provided (the
* handshake fires overlapping [updateChannelSettings] calls as it downloads channels one by one).
*/
Tf0883e@Single
Tff7b72class T56d364SwitchingChannelSetDataSourceTb4b4b4(
Tff7b72private Tff7b72val Te6edf3dbManagerTb4b4b4: Te6edf3DatabaseProviderTb4b4b4,
Tff7b72private Tff7b72val Te6edf3dispatchersTb4b4b4: Te6edf3CoroutineDispatchersTb4b4b4,
Tb4b4b4) Tb4b4b4{
Tff7b72private Tff7b72val Te6edf3writeMutex Tff7b72= Te6edf3MutexTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3channelSetFlowTb4b4b4: Te6edf3FlowTff7b72<Te6edf3ChannelSetTff7b72> Tff7b72=
Te6edf3dbManagerTb4b4b4.Te6edf3currentDb
Tb4b4b4.Te6edf3flatMapLatest Tb4b4b4{ Te6edf3db Tff7b72-Tff7b72> Te6edf3dbTb4b4b4.Te6edf3channelSetDaoTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3observeTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3map Tb4b4b4{ Te6edf3entity Tff7b72-Tff7b72> Te6edf3entityTff7b72?.Te6edf3channelSet Tff7b72?: Te6edf3ChannelSetTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3distinctUntilChangedTb4b4b4(Tb4b4b4)
Tff7b72suspend Tff7b72fun Td2a8ffclearChannelSetTb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// Must take writeMutex like mutate() -- otherwise a concurrent mutate() can read the row before this delete,
T8b949e// then upsert it back afterwards, silently undoing the clear.
Te6edf3withContextTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3ioTb4b4b4) Tb4b4b4{ Te6edf3writeMutexTb4b4b4.Te6edf3withLock Tb4b4b4{ Te6edf3dbManagerTb4b4b4.Te6edf3withDb Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3channelSetDaoTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3clearTb4b4b4(Tb4b4b4) Tb4b4b4} Tb4b4b4} Tb4b4b4}
Tb4b4b4}
T8b949e/** Replaces all [ChannelSettings] in a single atomic operation. */
Tff7b72suspend Tff7b72fun Td2a8ffreplaceAllSettingsTb4b4b4(Te6edf3settingsListTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4) Tb4b4b4{
Te6edf3mutate Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3copyTb4b4b4(Te6edf3settings Tff7b72= Te6edf3settingsListTb4b4b4) Tb4b4b4}
Tb4b4b4}
T8b949e/** Places [channel]'s settings at its index, resizing with blank channels to fill any gap (parity with legacy). */
Tff7b72suspend Tff7b72fun Td2a8ffupdateChannelSettingsTb4b4b4(Te6edf3channelTb4b4b4: Te6edf3ChannelTb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3channelTb4b4b4.Te6edf3role Tff7b72=Tff7b72= Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLEDTb4b4b4) Tff7b72return
Te6edf3mutate Tb4b4b4{ Te6edf3current Tff7b72-Tff7b72>
Tff7b72val Te6edf3settings Tff7b72= Te6edf3currentTb4b4b4.Te6edf3settingsTb4b4b4.Te6edf3toMutableListTb4b4b4(Tb4b4b4)
Tff7b72while Tb4b4b4(Te6edf3settingsTb4b4b4.Te6edf3size Tff7b72<Tff7b72= Te6edf3channelTb4b4b4.Te6edf3indexTb4b4b4) Tb4b4b4{
Te6edf3settingsTb4b4b4.Te6edf3addTb4b4b4(Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)Tb4b4b4)
Tb4b4b4}
Te6edf3settingsTff7b72[Te6edf3channelTb4b4b4.Te6edf3indexTff7b72] Tff7b72= Te6edf3channelTb4b4b4.Te6edf3settings Tff7b72?: Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)
Te6edf3currentTb4b4b4.Te6edf3copyTb4b4b4(Te6edf3settings Tff7b72= Te6edf3settingsTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tff7b72suspend Tff7b72fun Td2a8ffsetLoraConfigTb4b4b4(Te6edf3configTb4b4b4: Te6edf3ConfigTb4b4b4.Te6edf3LoRaConfigTb4b4b4) Tb4b4b4{
Te6edf3mutate Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3copyTb4b4b4(Te6edf3lora_config Tff7b72= Te6edf3configTb4b4b4) Tb4b4b4}
Tb4b4b4}
Tff7b72private Tff7b72suspend Tff7b72fun Td2a8ffmutateTb4b4b4(Te6edf3transformTb4b4b4: Tb4b4b4(Te6edf3ChannelSetTb4b4b4) Tff7b72-Tff7b72> Te6edf3ChannelSetTb4b4b4) Tb4b4b4{
Te6edf3withContextTb4b4b4(Te6edf3dispatchersTb4b4b4.Te6edf3ioTb4b4b4) Tb4b4b4{
Te6edf3writeMutexTb4b4b4.Te6edf3withLock Tb4b4b4{
Te6edf3dbManagerTb4b4b4.Te6edf3withDb Tb4b4b4{ Te6edf3db Tff7b72-Tff7b72>
Tff7b72val Te6edf3dao Tff7b72= Te6edf3dbTb4b4b4.Te6edf3channelSetDaoTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3current Tff7b72= Te6edf3daoTb4b4b4.Te6edf3getTb4b4b4(Tb4b4b4)Tff7b72?.Te6edf3channelSet Tff7b72?: Te6edf3ChannelSetTb4b4b4(Tb4b4b4)
Te6edf3daoTb4b4b4.Te6edf3upsertTb4b4b4(Te6edf3ChannelSetEntityTb4b4b4(Te6edf3channelSet Tff7b72= Te6edf3transformTb4b4b4(Te6edf3currentTb4b4b4)Tb4b4b4)Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.06s